package cn.luowenjie.faceclock;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import java.util.ArrayList;
import java.util.List;
import cn.luowenjie.faceclock.adapter.RecyclerAdapter;
import cn.luowenjie.faceclock.adapter.RecyclerViewDivider;
import cn.luowenjie.faceclock.clock.Clock;
import cn.luowenjie.faceclock.clock.FaceActivity;
import cn.luowenjie.faceclock.clock.SetClockActivity;
import cn.luowenjie.faceclock.database.DatabaseHelper;

import static cn.luowenjie.faceclock.R.*;

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private TextView toolbar_title;
    private RecyclerView recyclerView;
    private RecyclerAdapter adapter;
    private PopupWindow popupWindow;
    SharedPreferences sp;

    private PendingIntent pi;
    private AlarmManager alarmManager;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layout.activity_main);
        sp = getSharedPreferences("clocks", MODE_PRIVATE);
        initToolBar();
        initPopupWindow();
        setupList();
    }

    private void initPopupWindow() {
        View popupWindow_view = LayoutInflater.from(this).inflate(layout.popupwindow, null,false);
        popupWindow = new PopupWindow(popupWindow_view, ActionBar.LayoutParams.WRAP_CONTENT,
                ActionBar.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setAnimationStyle(R.style.AnimationPreview);
        popupWindow_view.findViewById(id.tttt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
                Intent intent = new Intent(MainActivity.this, SetClockActivity.class);
                intent.putExtra("mode", "add");
                startActivity(intent);
            }
        });



        popupWindow_view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
                return false;
            }

        });
    }

    private void initToolBar() {
        toolbar = findViewById(id.main_toolbar);
        toolbar_title = findViewById(id.toolbar_title);
        toolbar_title.setText("FaceClock");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        popupWindow.showAsDropDown(findViewById(id.title_add), -150, 60);
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (adapter != null) {
            adapter.saveStates(outState);
        }
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (adapter != null) {
            adapter.restoreStates(savedInstanceState);
        }
    }

    private void setupList() {
        recyclerView = (RecyclerView) findViewById(id.recycler_view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this,
                LinearLayoutManager.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new RecyclerViewDivider(this, LinearLayoutManager.VERTICAL));
        adapter = new RecyclerAdapter(this, getClockList());
        recyclerView.setAdapter(adapter);
    }

    private List<Clock> getClockList() {
        List<Clock> list = new ArrayList<Clock>();
        DatabaseHelper databaseHelper = new DatabaseHelper(this,"faceclock",null,1);
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select *" +
                " from clocks",null);
        while(cursor.moveToNext()){
            Integer id = cursor.getInt(0);
            Integer hour = cursor.getInt(1);
            Integer minute = cursor.getInt(2);
            Integer state = cursor.getInt(3);
            Boolean status = null;
            if(state == 1) {
                status = true;
            } else {status = false;}
            String tag = cursor.getString(4);
            Integer repeat = cursor.getInt(5);

            Clock clock = new Clock(id, hour, minute, status, tag, repeat);
            list.add(clock);

        }
        return list;
    }

    @Override
    protected void onResume() {
        adapter = null;
        adapter = new RecyclerAdapter(this, getClockList());
        recyclerView.setAdapter(adapter);
        super.onResume();
    }

}